
//
//  HTStatsCellViewModel.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/21.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit
import SDWebImage
class HTStatsCellViewModel: NSObject {

    var status : HTStats?{
    
        didSet{
        
            dealMeberImage()
            dealSourceText()
            
            
            if let s = status?.retweeted_status {
            
                if let name = s.user?.name, content = s.text {
                
                    retweetText = "@\(name):\(content)"
                }
            }
            
            retweetPictureViewSize = pictureSize(status?.retweeted_status?.pic_urls)
            originalPictureViewSize = pictureSize(status?.pic_urls)

            timeadd()
        }
    }
    
    var memberImage : UIImage?
    
    var retweetText : String?
    
    var sourceText : String?
    
    
    var createTime : String?
    
    //计算单张图片的大小
    
    
    
    
    
    //时间转换方法
    
    
    func timeadd(){
    
        guard let  createTimes = status?.created_at else{
            return
        }
        
        let  fm = NSDateFormatter()
        
        fm.locale = NSLocale(localeIdentifier: "en")
        
        fm.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        
        let createData = fm.dateFromString(createTimes)!
        
        //与当前时间进行对比
        if isDateInYears(createData){
        
            createTime = "今年"
        }else{
        
            createTime = "不知道什么时间"
        }
    }
    
    //时间转换方法封装
    func isDateInYears(targetDate : NSDate) -> Bool{
    
        let currentDate = NSDate()
        let fm = NSDateFormatter()
        fm.dateFormat = "yyyy"
        let targetYear = fm.stringFromDate(targetDate)
        let currentYear = fm.stringFromDate(currentDate)
        
        return targetYear == currentYear
        
    }
    
    
    func calcSinglenImage(pic_urls : [HTStatusPhotoInfo]?) -> CGSize{
    
    
        guard let imageUrlString = pic_urls?.first?.thumbnail_pic else{
        
            return CGSizeZero
        }
        
        if let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(imageUrlString){
        
            let  scale = UIScreen.mainScreen().scale
            var size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
            
            if size.width < 50 {
            
                let height = 50 / size.width * size.height
                size.width = 50
                size.height = height > 100 ? 100 :height
            }
            return size
            
            
        }
        return CGSizeZero
        
        
        
    }
    
    func updateSingleImagePictureView() {
        
        retweetPictureViewSize = calcSinglenImage(status?.retweeted_status?.pic_urls)
        originalPictureViewSize = calcSinglenImage(status?.pic_urls)
    }
    
    
    //微博转发视图大小
    var retweetPictureViewSize : CGSize = CGSizeZero
    var originalPictureViewSize: CGSize = CGSizeZero
    
    private func pictureSize(pic_urls : [HTStatusPhotoInfo]?) ->CGSize{
    
        let count = pic_urls?.count ?? 0
        
        if count == 0 {
            return CGSizeZero
        }
        
        let col = count == 4 ? 2 : (count > 3 ? 3 : count)
        
        let row = ((count - 1) / 3) + 1
        
        //求出每一张图片的大小
        let margin : CGFloat = 5
        
        let itemW = (UIScreen.mainScreen().bounds.width - 2 * HMStatusCellMargin - 2 * margin) / 3
        
        let width = CGFloat(col) * itemW + CGFloat(col - 1) * margin
        let hight = CGFloat(row) * itemW + CGFloat(row - 1) * margin
        
        return CGSize(width: width, height: hight)
    }
    
    
    
    
    //截取字符串 来自于那个设备
    private func dealSourceText(){
    
        guard let s = status?.source else {
            return
        }
        
        if let startIndex = s.rangeOfString("\">")?.endIndex, endIndex = s.rangeOfString("</")?.startIndex {
            let result = s.substringWithRange(startIndex..<endIndex)
            sourceText = "来自 \(result)"
        }
    }
    
    private func dealMeberImage(){
    
        if let rank = status?.user?.mbrank where(rank > 0 && rank < 7){
        
        memberImage =  UIImage(named: "common_icon_membership_level\(rank)")
            
        }
    }

}
